<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <ColumnTree class="w-1/4 xl:w-1/6" @select="handleSelect" />
    <BasicTable @register="registerTable" class="w-3/4 xl:w-5/6" :searchInfo="searchInfo">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate">商品发布</a-button>
        <!-- <a-button type="primary" @click="handleExport">导出账号</a-button> -->
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'imgs'">
          <TableImg :size="60" :simpleShow="true" :imgList="imgs" />
        </template>
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'clarity:info-standard-line',
                tooltip: '查看商品详情',
                onClick: handleView.bind(null, record),
              },
              {
                icon: 'clarity:note-edit-line',
                tooltip: '编辑商品信息',
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                tooltip: '删除此商品',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <GoodsDrawer @register="registerDrawer" @success="handleSuccess" />
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { reactive } from 'vue';

  import { BasicTable, useTable, TableAction, TableImg } from '@/components/Table';
  import { getGoodsList } from '@/api/demo/shop';
  import { PageWrapper } from '@/components/Page';
  import ColumnTree from './ColumnTree.vue';

  import { useDrawer } from '@/components/Drawer';
  import GoodsDrawer from './GoodsDrawer.vue';

  import { columns, searchFormSchema } from './goods.data';
  import { useGo } from '@/hooks/web/usePage';

  defineOptions({ name: 'GoodsManagement' });

  const imgs = [ "http://dummyimage.com/800x600/f279c4/79e7f2&text=Kgnksyvx Nfwijte Kdgc Qksyy", "http://dummyimage.com/800x600/f2d979/b679f2&text=Lanjfrsj Ssjnfknf Tfrrlrj Uxmavtgaqx Qconf", "http://dummyimage.com/800x600/79f292/f27982&text=Sujqcoba Bqeeil Kihfvi Djnckw Tghzlwbc Ntmhdlkgp" ];
 
  const go = useGo();
  const [registerDrawer, { openDrawer }] = useDrawer();
  const searchInfo = reactive<Recordable>({});
  const [registerTable, { reload, updateTableDataRecord, getSearchInfo }] = useTable({
    title: '商品列表',
    api: getGoodsList,
    rowKey: 'id',
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
    },
    useSearchForm: true,
    showTableSetting: true,
    showIndexColumn: false,
    bordered: true,
    handleSearchInfoFn(info) {
      console.log('handleSearchInfoFn', info);
      return info;
    },
    actionColumn: {
      width: 120,
      title: '操作',
      dataIndex: 'action',
      // slots: { customRender: 'action' },
    },
  });

  function handleCreate() {
    openDrawer(true, {
      isUpdate: false,
    });
  }

  function handleEdit(record: Recordable) {
    openDrawer(true, {
      record,
      isUpdate: true,
    });
  }

  function handleDelete(record: Recordable) {
    console.log(record);
  }

  function handleExport() {
    console.log(getSearchInfo());
  }

  function handleSuccess({ isUpdate, values }) {
    if (isUpdate) {
      // 演示不刷新表格直接更新内部数据。
      // 注意：updateTableDataRecord要求表格的rowKey属性为string并且存在于每一行的record的keys中
      const result = updateTableDataRecord(values.id, values);
      console.log(result);
    } else {
      reload();
    }
  }

  function handleSelect(deptId = '') {
    searchInfo.deptId = deptId;
    reload();
  }

  function handleView(record: Recordable) {
    go('/shop/goods/detail/' + record.id);
  }
</script>
